//
//  LXUserAccount.swift
//  weibo
//
//  Created by lixin on 16/4/19.
//  Copyright © 2016年 lixin. All rights reserved.
//

import UIKit

class LXUserAccount: NSObject, NSCoding {

    var access_token: String?
    
    var expires_date: NSDate?//自己定义的用来判断token是否过期的标识
    
    var expires_in: Int = 0 {
    
        didSet{
        
            expires_date = NSDate().dateByAddingTimeInterval(Double(expires_in))
        }
    }
    
    var uid: String?//授权用户的uid
    
    var avatar_large: String?//头像
    
    var screen_name: String?//昵称
    
    init(dict:[String: AnyObject]) {
        
        super.init()
        
        setValuesForKeysWithDictionary(dict)
    }
    
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {
        
    }
    
    override var description: String{
    
        let key = ["access_token", "expires_date", "uid", "avatar_large", "screen_name"]
        
        return dictionaryWithValuesForKeys(key).description
    }
    
    //归档
    func encodeWithCoder(aCoder: NSCoder) {
        
        aCoder.encodeObject(access_token, forKey: "access_token")
        
        aCoder.encodeObject(expires_date, forKey: "expires_date")
        
        aCoder.encodeObject(uid, forKey: "uid")
        
        aCoder.encodeObject(avatar_large, forKey: "avatar_large")
        
        aCoder.encodeObject(screen_name, forKey: "screen_name")
    }
    
    //解档
    required init?(coder aDecoder: NSCoder) {
        
        access_token = aDecoder.decodeObjectForKey("access_token") as? String
        
        expires_date = aDecoder.decodeObjectForKey("expires_date") as? NSDate
        
        uid = aDecoder.decodeObjectForKey("uid") as? String
        
        avatar_large = aDecoder.decodeObjectForKey("avatar_large") as? String
        
        screen_name = aDecoder.decodeObjectForKey("screen_name") as? String
    }
}
